package com.ys.controller.api.hr;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.config.MenuId;
import com.ys.entity.hr.TbJobLevel;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.hr.TbJobLevelService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService.UploadDto;
import com.ys.util.ExportExcelUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

@Controller
@RequestMapping("/api/inter/tbJobLevel")
public class ApiInterTbJobLevelController {
	
//	private static final Logger logger = Logger.getLogger(ApiAdminLoginController.class);
	
	@Autowired
	private CommonApiService<TbJobLevel> commonApiService;
	@Autowired
	private TbJobLevelService tbJobLevelService;
	
	private static final Logger logger = LoggerFactory.getLogger(ApiInterTbJobLevelController.class);
	@RequiresPermissions("tbJobLevel:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(@ModelAttribute TbJobLevel cond){
		cond.setOrderby("A.Id DESC");
		return commonApiService.searchLay(cond);
	}
	/**
	 * 新增
	 */
	@RequestMapping("/add")
	@ResponseBody
	@RequiresPermissions("tbJobLevel:add")
	public R add(@RequestParam Map<String, Object> params) {
		try {
			logger.info("岗位职级管理新增传参：" + params);
			//修改
			return tbJobLevelService.add(params);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
		}
	}

	/**
	 * 编辑
	 */
	@RequiresPermissions("tbJobLevel:edit")
	@PostMapping("/edit")
    @ResponseBody
	public R edit(@RequestParam Map<String, Object> params){
		try {
			return tbJobLevelService.edit(params);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
		}
	}
	
	/*
	 * 导出
	 */
	@RequiresPermissions("tbJobLevel:export")
	@RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TbJobLevel cond) throws Exception{
		cond.setOrderby("A.Id DESC");
		
		cond.setPage(0).setSize(999999);

		List<Map<String, Object>> listData = cond.queryMap();
        
    	List<YsCol> listField = new YsCol()
    			.setMenuId(MenuId.TB_JobLevel)
    			.setFrontShow(YsColEnum.FrontShow.SHOW.value)
    			.setOrderby("A.SortNo asc")
    			.queryList();
    	
    	
        ExportExcelUtils<TbJobLevel> util = new ExportExcelUtils<TbJobLevel>(new TbJobLevel());
        
        String toFilePath = util.exportExcel(listData, listField, "岗位职级管理");
        UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
        return R.ok(dto.getUrl());
    }

	/**
	 * 弹出框
	 */
	@RequestMapping("/searchForModal")
	@ResponseBody
	public LayuiTableVoJA searchForModal(@ModelAttribute TbJobLevel cond){
		//岗位类别(0管理 1服务 2技术)
		cond.addField("CASE " +
				"WHEN A.Category = 0 THEN '管理' " +
				"WHEN A.Category = 1 THEN '服务' " +
				"WHEN A.Category = 2 THEN '技术' " +
				" ELSE '' END categoryTxt");
		//职级大类(0A 1B 2C 3D 4P)
		cond.addField("CASE " +
				"WHEN A.Categories = 1 THEN 'A' " +
				"WHEN A.Categories = 2 THEN 'B' " +
				"WHEN A.Categories = 3 THEN 'C' " +
				"WHEN A.Categories = 4 THEN 'D' " +
				"WHEN A.Categories = 5 THEN 'P' " +
				" ELSE '' END categoriesTxt");
		//岗位工种(0总经理 1副总经理 2责任月薪型员工 3绩效提奖型员工 4研发人员)
		cond.addField("CASE " +
				"WHEN A.Type = 1 THEN '总经理' " +
				"WHEN A.Type = 2 THEN '副总经理' " +
				"WHEN A.Type = 3 THEN '责任月薪型员工' " +
				"WHEN A.Type = 4 THEN '绩效提奖型员工' " +
				"WHEN A.Type = 5 THEN '研发人员' " +
				" ELSE '' END roleLevelTypeTxt");
		cond.setStatus(1);
		return commonApiService.searchLay(cond);
	}

}
